<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/7/17
 * Time: 10:51
 */
namespace app\home\controller;
//include '/data/wwwroot/health.xuanwumobile.com/vendor/autoload.php'; // 引入 composer 入口文件
use think\Db;
use think\Config;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use think\Controller;
class My extends Common
{
    /**
     * 购物车首页
     */
    public function index()
    {
        $auth = $this->checkAll();
        $openid = $this->getOpenid();
        $user = Db::table('up_u_user')
            ->where('user_openid', $openid)
            ->find();
        if(!$user){
            return $this->fetch('login/login');
        }else{
            $user_id = $user['user_id'];
            $data['noPay'] = Db::table('up_order')
                ->where('order_userid', $user_id)
                ->where('order_status', 1)
                ->where('order_in_out', 1)
                ->count();
            $data['havePay'] = Db::table('up_order')
                ->where('order_userid', $user_id)
                ->where('order_status', 2)
                ->where('order_in_out', 1)
                ->count();
            $data['noComment'] = Db::table('up_order')
                ->where('order_userid', $user_id)
                ->where('order_status', 3)
                ->where('order_in_out', 1)
                ->count();
            $this->updateOneToDel($user_id);
            $this->assign('user',$user);
            $this->assign('data',$data);
            return $this->fetch('my/index');
        }
    }
    /**
     * 个人中心
     */
    public function userSet()
    {
        $auth = $this->checkAll();
        $openid = $this->getOpenid();
        $user = Db::table('up_u_user')
            ->where('user_openid', $openid)
            ->find();
        if(!$user){
            return $this->fetch('login/login');
        }else{
            //-----------------获取微信位置接口-----------------------
            $options = Config::get('wechat');
            $app = new Application($options);
            $js = $app->js;
            $json = $js->config(array('getLocation','chooseImage','uploadImage','downloadImage','getLocalImgData'), false ,false,true);
            $this->assign(['json'=>$json]);
            //-----------------------end-----------------------
            $this->assign('user',$user);
            return $this->fetch('my/user_set');
        }
    }
    /**
     * 个人中心
     */
    public function updateName()
    {
        extract(input());
        $auth = $this->checkAll();
        $user = Db::table('up_u_user')
            ->where('user_id',$uid)
            ->find();
        $this->assign('user',$user);
        return $this->fetch('my/user_name');
    }
    public function AjaxUserCenter()
    {
        $uid = $_POST['uid'];
        $name = $_POST['name'];
        $value = $_POST['value'];
        $update = array();
        if($name == 'user_image'){
            $update["user_image"] = base64_img($value)['msg'];;
        }else{
            $update["$name"] = $value;
        }
        $res = Db::table('up_u_user')
            ->where('user_id',$uid)
            ->update($update);
        if($res){
            return json(['status'=>200,'msg'=>'ok']);
        }else{
            return json(['status'=>0,'msg'=>'更改失败']);
        }
    }
    /**
     *
     * @return \think\response\Json
     */
    public function address(){
        extract(input());
        $auth = $this->checkAll();
        //-------------------------------------------------
        $openid = $this->getOpenid();
        $user = Db::table('up_u_user')
            ->where('user_openid', $openid)
            ->find();
        if(!$user){
            return $this->fetch('login/login');
        }else{
            $user_id = $user['user_id'];
            $address = Db::table('up_user_address')//存在即代表有抢购商品（秒杀）
            ->where('add_userid', $user_id)
                ->order('add_id DESC')
                ->select();
            $this->assign('address',$address);
            $this->assign('type',2);
            return $this->fetch('my/address');
        }
        //-------------------------------------------------
    }
    /**
     * @return \think\response\Json
     */
    public function addAdd(){
        extract(input());
        $addEdit = 1;
        $data = array();
        $data['name'] = '';
        $data['phone'] = '';
        $data['sex'] = '';
        $data['lat'] = '';
        $data['lon'] = '';
        $data['address'] = '';
        $data['door'] = '';
        if(isset($addid)){
            $address2 = Db::table('up_user_address')//存在即代表有抢购商品（秒杀）
            ->where('add_id', $addid)->find();
            $data['name'] = $address2['add_username'];
            $data['phone'] = $address2['add_phone'];
            $data['sex'] = $address2['add_sex'];
            $data['lat'] = $address2['add_lat'];
            $data['lon'] = $address2['add_lon'];
            $data['address'] = $address2['add_address'];
            $addEdit = 2;
            $this->assign('addid',$addid);
        }
        if(isset($name)){$data['name'] = $name;}
        if(isset($phone)){$data['phone'] = $phone;}
        if(isset($sex)){$data['sex'] = $sex;}
        if(isset($lat)){$data['lat'] = $lat;}
        if(isset($lon)){$data['lon'] = $lon;}
        if(isset($address)){$data['address'] = $address;}
        if(isset($door)){$data['door'] = $door;}
        $this->assign('data',$data);
        $this->assign('type',2);
        $this->assign('addEdit',$addEdit);
        return $this->fetch('my/add_address');
    }
    public function addressLocation(){
        extract(input());
        $addEdit = 1;
        $data = array();
        $data['name'] = '';
        $data['phone'] = '';
        $data['sex'] = '';
        $data['lat'] = '';
        $data['lon'] = '';
        $data['address'] = '';
        $data['door'] = '';
        if(isset($addid)){
            $address2 = Db::table('up_user_address')//存在即代表有抢购商品（秒杀）
            ->where('add_id', $addid)->find();
            $data['name'] = $address2['add_username'];
            $data['phone'] = $address2['add_phone'];
            $data['sex'] = $address2['add_sex'];
            $data['lat'] = $address2['add_lat'];
            $data['lon'] = $address2['add_lon'];
            $data['address'] = $address2['add_address'];
            $addEdit = 2;
            $this->assign('addid',$addid);
        }
        if(isset($name)){$data['name'] = $name;}
        if(isset($phone)){$data['phone'] = $phone;}
        if(isset($sex)){$data['sex'] = $sex;}
        if(isset($lat)){$data['lat'] = $lat;}
        if(isset($lon)){$data['lon'] = $lon;}
        if(isset($address)){$data['address'] = $address;}
        if(isset($door)){$data['door'] = $door;}
        $this->assign('type',2);
        $this->assign('data',$data);
        $this->assign('addEdit',$addEdit);
        return $this->fetch('my/location_address');
    }
    public function editMyAddress(){
        extract(input());
        $data = array();
        $data['add_lat'] = $lat;
        $data['add_lon'] = $lon;
        $data['add_address'] = $address.$door;
        $data['add_phone'] = $phone;
        $data['add_username'] = $name;
        $data['add_sex'] = $sex;
        $res = Db::table('up_user_address')->where('add_id',$addid)->update($data);
        if($res){
            return json(['status'=>200,'msg'=>'成功']);
        }else{
            return json(['status'=>0,'msg'=>'失败']);
        }
    }
    public function delMyAddress(){
        extract(input());
        $res = Db::table('up_user_address')->where('add_id',$addid)->delete();
        if($res){
            return json(['status'=>200,'msg'=>'成功']);
        }else{
            return json(['status'=>0,'msg'=>'失败']);
        }
    }
    /**
     * 设置
     */
    public function set(){
        extract(input());
        $auth = $this->checkAll();
        return $this->fetch('my/set');
    }
    /**
     * 密码修改
     */
    public function resetPass(){
        extract(input());
        $auth = $this->checkAll();
        return $this->fetch('my/reset_pass');
    }
    /**
     * 关于我们
     */
    public function about(){
        extract(input());
        $auth = $this->checkAll();
        $data = Db::table('up_about')->where('a_id',1)->find();
        $this->assign('data',$data);
        return $this->fetch('my/about');
    }
    /**
     * 意见反馈
     */
    public function feedback(){
        extract(input());
        $auth = $this->checkAll();
        $data = Db::table('up_feedback_cate')
            ->field('cate_id,cate_name')
            ->select();
        $this->assign('data',$data);
        return $this->fetch('my/feedback');
    }
    /**
     * 新增意见反馈
     * $content,$phone,$type
     */
    public function addfeedback(){
        extract(input());
        $openid = $this->getOpenid();
        $user_id = Db::table('up_u_user')
            ->where('user_openid', $openid)
            ->value('user_id');
        $auth = $this->checkAll();
        $data = array();
        $data['f_user_id'] = $user_id;
        $data['f_type'] = trim($type,',');
        $data['f_content'] = $content;
        $data['f_phone'] = $phone;
        $data['f_time'] = date('Y-m-d H:i:s');
        $res = Db::table('up_feedback')->insert($data);
        if($res){
            return json(['status'=>200,'msg'=>'提交成功']);
        }else{
            return json(['status'=>0,'msg'=>'提交失败']);
        }
    }
}